/**
    ******************************************************************************
    * @file    main.cpp
    * @author  debian
    * @version V1.0.0
    * @date    2020-03-22
    * @brief   zmq测试程序
    ******************************************************************************
    * @attention
    *
    *
    ******************************************************************************
    */ 

/* Includes ------------------------------------------------------------------*/
#include "main.h"


/** @addtogroup DataStruct_Driver
    * @{
    */

/** @addtogroup main
    * @{
    */

/* Private typedef -----------------------------------------------------------*/

/* Private define ------------------------------------------------------------*/

/* Private macro -------------------------------------------------------------*/

/* Private variables ---------------------------------------------------------*/

/* Private function prototypes -----------------------------------------------*/

/* Private functions ---------------------------------------------------------*/


/** @defgroup main_Exported_Functions main Exported Functions
    * @{
    */

/** @defgroup main_Exported_Functions_Group1 Initialization and deinitialization functions
    *  @brief    Initialization and Configuration functions
    *
@verbatim    
    ===============================================================================
                ##### Initialization and deinitialization functions #####
    ===============================================================================
    [..]
        This section provides functions allowing to initialize and de-initialize the main
        to be ready for use.
 
@endverbatim
    * @{
    */ 
#include <iostream>
#include <string.h>
#include "zmq.hpp"
#include <unistd.h>
#include <arpa/inet.h>
#include <stdio.h>

using namespace std;
// export LD_LIBRARY_PATH="&LD_LIBRARY_PATH:/usr/local/lib"
// g++ main.cpp -lzmq -o main
/**
    * @brief  Main构造函数
    * @param  
    * @retval 
    */ 
char file_buff[1024];
int file_one_read = 1024;

int main(int argc, char **argv)
{

    if(argc != 3) {
        return -1;
    }

    char *ip = argv[1];
    char *file = argv[2];
    printf("ip = %s %s\n", ip, file);
    zmq::context_t context (1);
    #if 0
    zmq::socket_t socket(context, ZMQ_PUSH);
    char buff[1024] = "Hello, world";

    socket.connect("tcp://122.51.111.216:5555");

    zmq::message_t mess(buff, strlen(buff));
    socket.send(mess);
    //socket.send(buff, strlen(buff));
    //socket.recv(buff, 1024);
    #endif

    #if 0
     zmq::socket_t socket(context, ZMQ_SUB);
     //socket.connect("tcp://122.51.111.216:5556");
     socket.connect("tcp://122.51.111.216:5555");

     socket.setsockopt(ZMQ_SUBSCRIBE, "", 0);    //全部消息都收到
     //socket.setsockopt(ZMQ_SUBSCRIBE, "order ", strlen("order "));   //只收到order消息

     for(int i=0; i<10; i++) {
         zmq::message_t mess;
        socket.recv(&mess);

        printf("接收到的数据 %s\n", (char *)zmq_msg_data(mess.handle()));
     }
  
    socket.setsockopt(ZMQ_UNSUBSCRIBE, "order ", strlen("order "));     //取消订阅
    for(int i=0; i<100; i++) {
         zmq::message_t mess;
        socket.recv(&mess);

        printf("取消后接收到的数据 %s\n", (char *)zmq_msg_data(mess.handle()));
     }
    #endif

    char buff[200];
    snprintf(buff, sizeof(buff), "tcp://%s", ip);
    zmq::socket_t socket(context, ZMQ_PUSH);
    socket.setsockopt(ZMQ_SNDTIMEO, 0);
    //socket.connect(buff);
    socket.connect("tcp://122.51.111.216:5555");

    sleep(1);

    zmq::message_t mess(128);
    long file_path_len = strlen(file);
    file_path_len = htonl(file_path_len);
    printf("file %d %s\n", file_path_len, file);
    socket.send(&file_path_len, 4, ZMQ_SNDMORE);
    socket.send(file, strlen(file), ZMQ_SNDMORE);
    //socket.send(mess, ZMQ_SNDMORE);

    FILE *fp = fopen(file, "rb");
    if(fp == NULL) {
        printf("file no exist\n");
        return -1;
    }

    int size = 0;
    int pos = size;
    int count = 0;
    while(1)
    {
        fseek(fp, pos, SEEK_SET);
        size = fread(file_buff, 1, sizeof(file_buff), fp);
        if(size < 0) {
            printf("read file fail\n");
            socket.send(file_buff, 0, 0);
            break;
        } else if(size == file_one_read) {
            socket.send(file_buff, size, ZMQ_SNDMORE);
            printf("send %d\n", count++);
        } else if(size < file_one_read) {
            socket.send(file_buff, size, 0);
            printf("send file succces %s\n", file_buff);
            break;
        }
        pos += size;
    }

    fclose(fp);

    socket.close();
    context.close();

    return 0;
}

/**
    * @brief  Main析构函数
    * @param  
    * @retval 
    */ 
//g++ main.cpp -lzmq -Wl,-rpath=/usr/local/lib -o main
    
/**
    * @}
    */

/** @defgroup main_Exported_Functions_Group2 operation functions 
    *  @brief   operation functions
    *
@verbatim   
    ===============================================================================
                        ##### operation functions #####
    ===============================================================================
    [..]
        This subsection provides a set of functions allowing to manage the main.

@endverbatim
    * @{
    */

        /* 操作函数写在这里 */

    /**
    * @}
    */


/**
    * @}
    */

/**
    * @}
    */

/**
    * @}
    */

/************************ (C) 2020 HOME 深圳龙华 *****END OF FILE****/

